<?php
namespace app\admin\model;


use think\model;


class Goods extends  Model
{
         protected $pk = 'goods_id';
         protected $error;
         public function getError()  //错误信息
         {
             return $this->error;
         }


    public function searchGoodsAttr($query,$value,$data)
         {
             if (''!== $value){
                 $query->whereLike('goods_name','%'.$value.'%');
             }
         }
         public function searchIfShowAttr($query,$value,$data)
         {
             if (is_array($value) || (is_scalar($value) && strpos('#'.$value,','))){
                 $query->whereIn('ecm_goods.if_show',$value);
             }
             if (is_scalar($value) && false===strpos($value,',') && intval($value) <2)
             {
                 $query->where('ecm_goods.if_show',intval($value));
             }
         }
         public function searchRecommendedAttr($query,$value,$data)
         {
             if (is_array($value)||(is_scalar($value) && strpos('#'.$value,','))){
                 $query->whereIn('ecm_goods.recommended',$value);
             }
             if (is_scalar($value) && false === strpos($value,',')&& intval($value) < 2){
                 $query->where('ecm_goods.recommended',intval($value));
             }
         }


        public function searchSmallAttr($query,$value){
            if(!empty($value)){
                $query->whereTime('ecm_goods.add_time','>=',$value);
            }
        }
        public function searchBigAttr($query,$value){
            if(!empty($value)){
                $query->whereTime('ecm_goods.add_time','<=',$value);
            }
        }

        public function searchAddTimeAttr($query,$value){
            if(!empty($value) && is_array($value) && 2 === count($value)){
                $query->whereBetweenTime('ecm_goods.add_time',$value[0],$value[1]);
            }
        }
        public function searchCateIdAttr($query,$value)
        {

        }





         //构造器
        public function searchPageAttr($query,$value){}

         public static function getGoodList($data = [],$query=[],$page = false)
         {
             echo "<pre>";
             print_r($data);
             echo "</pre>";
             $model = self::field(true)
                 ->withSearch(array_keys($data),$data);
//             if (isset($data['goods']) && ''!==$data['goods']){
//                 $model->whereLike('goods_name','%'.$data['goods'].'%');
//             }
////              if (isset($data['if_show']) &&'2'!==$data['if_show']){
////                  $model->where('if_show','=',$data['if_show']);
////              }
//             if(isset($data['if_show']) && is_array($data['if_show'])){
//                 $model->whereIn('ecm_goods.if_show',$data['if_show']);
//             }
//
//             if(isset($data['if_show']) && is_string($data['if_show']) &&  strpos('#'.$data['if_show'],',')){
//                 $model->whereIn('ecm_goods.if_show',$data['if_show']);
//             }
//
//             if(isset($data['if_show']) && is_string($data['if_show']) &&  false === strpos($data['if_show'],',')){
//                 $model->where('ecm_goods.if_show',$data['if_show']);
//             }
//              if (isset($data['recommended']) && ''!==$data['recommended']){
//                  $model->where('recommended','=',$data['recommended']);
//              }
//              if (isset($data['small']) && !empty($data['small'])){
//                  $model->whereTime('add_time','>=',$data['small']);
//              }
//             if (isset($data['big']) && !empty($data['big'])){
//                 $model->whereTime('add_time','<=',$data['big']);
//             }
             if (isset($data['cate_id']) && ''!==$data['cate_id']){
                 $model->where('cate_id','=',$data['cate_id']);
             }


             // 排序
             if(isset($query['sort']) && 'desc' == $query['sort']){
                 $model->order('add_time','desc');
             }elseif(isset($query['sort']) && 'asc' == $query['sort']){
                 $model->order('add_time','asc');
             }else{
                 $model->order('goods_id','desc');
             }



             if ($page){
                 $result = $model->select();
             }else{
                 $result = $model->paginate([
                     'list_rows' =>5,
                     'query'     =>$query,
                 ]);
             }
             return $result;


         }
    public function add(array $data,array $allowField = [],bool $replace = false)  //商品添加
    {
        $data['add_time'] = time();
        $data['last_update'] = time();
        try {
            if (empty($data)) {
                $this->error = '数据不能为空!';
              
            }
            if (!empty($allowField)) {
                $this->allowField($allowField);
            }
            $this->replace($replace)->save($data);
            $id = $this->toArray();
//            echo "<pre>";
//            print_r($id);
//            echo "</pre>";
//            exit;
            return $id[$this->pk];
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }
    }

}